//
//  DraftEntity.m
//  Weibbo
//
//  Created by ifuninfo on 13-6-9.
//  Copyright (c) 2013年 Omnivores. All rights reserved.
//

#import "DraftEntity.h"
#import "NSString+MD5Addition.h"
#import "DraftItem.h"

@interface DraftEntity()
@end

@implementation DraftEntity
@synthesize entityId = _entityId;
@synthesize titleText = _titleText;
@synthesize titleImageName=_titleImageName;
@synthesize createTime = _createTime;
@synthesize modifyTime = _modifyTime;
@synthesize draftList = _draftList;
@synthesize canBeModified = _canbeModified;
- (id)init
{
    self = [super init];
    if (self)
    {
        NSTimeInterval  currentTime = [[NSDate date] timeIntervalSince1970];
        NSString *referanceId = [NSString stringWithFormat:@"%f%@",currentTime,[[self class] description]];
        _entityId = [[referanceId stringFromMD5] retain];
        self.createTime = currentTime;
        self.modifyTime = currentTime;
        self.draftList = [NSMutableArray array];
        self.canBeModified = YES;
        self.titleImageName = @"pre_head_bg.png";
        self.titleText = @"很长很长的微博";
    }
    return self;
}
-(id)initWithInfo:(NSDictionary *)info
{
    self = [super init];
    if (self)
    {
        self.titleText  = [info objectForKey:@"title"];
        self.titleImageName = [info objectForKey:@"head_image"];
        self.createTime = [[info objectForKey:@"create_time"] doubleValue];
        self.modifyTime = [[info objectForKey:@"modify_time"] doubleValue];
        self.canBeModified = [[info objectForKey:@"canbe_modfy"] boolValue];
        self.entityId = [info objectForKey:@"id"];
        self.draftList = [NSMutableArray array];
        NSMutableArray  *dicList = [info objectForKey:@"article_list"];
        for(NSDictionary  *dic in dicList)
        {
            DraftItem  *item = [[DraftItem alloc] initWithDictionary:dic];
            [_draftList addObject:item];
            [item release];
        }
    }
    return self;
}
- (NSMutableDictionary *)toDictionary
{
    NSMutableDictionary  *result = [NSMutableDictionary dictionary];
    [result setObject:self.titleText forKey:@"title"];
    [result setObject:self.titleImageName forKey:@"head_image"];
    [result setObject:[NSString stringWithFormat:@"%f",self.createTime] forKey:@"create_time"];
    [result setObject:[NSString stringWithFormat:@"%f",self.modifyTime] forKey:@"modify_time"];
    [result setObject:[NSString stringWithFormat:@"%d",self.canBeModified] forKey:@"canbe_modfy"];
    
    NSMutableArray *draftList = [NSMutableArray array];
    for(DraftItem *item in self.draftList)
    {
        [draftList addObject:[item toDictionary]];
    }
    [result setObject:draftList forKey:@"article_list"];
    [result setObject:_entityId forKey:@"id"];
    return result;
}

-(void)setModifyTime:(NSTimeInterval)modifyTime
{
    _modifyTime = modifyTime;
}
-(void)synchronise
{
    int index = [WBBDRAFT indexOfEntity:self];
    [WBBDRAFT replaceDraft:self index:index];
}
- (int)count
{
    return [_draftList count];
}
- (void)dealloc
{
    UIRelease(_draftList);
    UIRelease(_entityId);
    UIRelease(_titleText);
    UIRelease(_titleImageName);
    UIRelease(_draftList);
    [super dealloc];
}
@end
